package service

import (
	"illuminant/model"

	"github.com/gohouse/gorose/v2"
	"github.com/rs/xid"
)

func GetTodos() ([]model.Todo, error) {
	var todos []model.Todo
	db := model.DB()
	err := db.Table(&todos).Select()
	return todos, err
}

func CreateTodo(name string, finished int) error {
	db := model.DB()
	id := xid.New().String()
	var todo model.Todo
	_, err := db.Table(todo.TableName()).Data(gorose.Data{
		"id":       id,
		"name":     name,
		"finished": finished,
	}).Insert()

	return err
}

func DeleteTodo(id string) error {
	db := model.DB()
	var t model.Todo
	_, err := db.Table(t.TableName()).Where("id", id).Delete()
	return err
}
